package net.sppan.base.controller.admin.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sppan.base.common.RedisKey;
import net.sppan.base.config.redis.RedisTemplateUtil;
import net.sppan.base.controller.modle.GetContentUnreviewedReq;
import net.sppan.base.controller.webSocket.WebSocketServer;
import net.sppan.base.entity.ContentExuser;
import net.sppan.base.entity.DyContexts;
import net.sppan.base.service.IConfigureParameterService;
import net.sppan.base.service.IContentExuserService;
import net.sppan.base.service.specification.SimpleSpecificationBuilder;
import net.sppan.base.service.specification.SpecificationOperator.Operator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExdyController {
    
	 
	@Autowired
	private IContentExuserService contentExuserService; 
	
	@Autowired
	private RedisTemplateUtil redisTemplateUtil;
	@Autowired
	private IConfigureParameterService configureParameterService;
	
	
	@ResponseBody
	@GetMapping(value = { "/countent/examine/getContentUnreviewed" })
	public Map<String, Object> getdyExaminContent(String dyID) throws InterruptedException{
		Map<String, Object> map = new HashMap<String, Object>();
		if(dyID!=null&&!"".equals(dyID.trim())){
			boolean b =configureParameterService.getAuth(dyID.trim());
		    if(b){
				Long lastContent=999999999999999L;
				
				String value = (String) redisTemplateUtil.get(RedisKey.last_red_key+dyID.trim());
				if(value!=null&&!value.equals("")){
					lastContent =Long.valueOf(value);
				}
				Page<DyContexts> contentExusers=contentExuserService.findContentExuserByDyId(dyID.trim(),lastContent,   PageRequest.of(0, 10,Direction.DESC, "uploadDate"));
		        if(contentExusers.getContent().size()==0){//在没有审核分配下 进行延迟  先进行分配再返回
		        	contentExuserService.distributionBy(null,dyID.trim(),0);
		        	Thread.sleep(2000);
		        	contentExusers=contentExuserService.findContentExuserByDyId(dyID.trim(), lastContent,PageRequest.of(0, 10, Direction.DESC, "uploadDate"));
				}else if(contentExusers.getTotalElements()>0 && contentExusers.getTotalElements()<=20){
				 	contentExuserService.distributionBy(null,dyID.trim(), (int)contentExusers.getTotalElements());
				} 
		        if(contentExusers.getNumberOfElements()>0){//
		        	redisTemplateUtil.set(RedisKey.last_red_key+dyID.trim(),String.valueOf(contentExusers.getContent().get((contentExusers.getNumberOfElements())-1).getDyContextID()));
		        }else{
		        	redisTemplateUtil.del(RedisKey.last_red_key+dyID.trim());
		        }
		        map.put("dyContexts",  contentExusers.getContent());
		    }else{
		    	map.put("dyContexts",new ArrayList<DyContexts>());
		    }
		}else{
			map.put("dyContexts",new ArrayList<DyContexts>());
		}
        map.put("respMessage", "OK");
        map.put("respCode", "0");
        map.put("isUsed", true);
		return map;
	} 
	
	
	
	
	@ResponseBody
	@PostMapping(value = { "/countent/examine/getContentUnreviewed" })
	public Map<String, Object> dyExaminContent(@RequestBody GetContentUnreviewedReq contentUnreviewedReq) throws InterruptedException{ 
	    Map<String, Object> map = new HashMap<String, Object>();
		if(contentUnreviewedReq!=null&&!contentUnreviewedReq.getDyID().equals("")){
			boolean b =configureParameterService.getAuth(contentUnreviewedReq.getDyID());
		    if(b){
				Long lastContent=999999999999999L;
				
				String value = (String) redisTemplateUtil.get(RedisKey.last_red_key+contentUnreviewedReq.getDyID());
				if(value!=null&&!value.equals("")){
					lastContent =Long.valueOf(value);
				}
				Page<DyContexts> contentExusers=contentExuserService.findContentExuserByDyId(contentUnreviewedReq.getDyID(),lastContent,   PageRequest.of(0, 10,Direction.DESC, "uploadDate"));
		        if(contentExusers.getContent().size()==0){//在没有审核分配下 进行延迟  先进行分配再返回
		        	contentExuserService.distributionBy(null,contentUnreviewedReq.getDyID(),0);
		        	Thread.sleep(2000);
		        	contentExusers=contentExuserService.findContentExuserByDyId(contentUnreviewedReq.getDyID(), lastContent,PageRequest.of(0, 10, Direction.DESC, "uploadDate"));
				}else if(contentExusers.getTotalElements()>0 && contentExusers.getTotalElements()<=20){
				 	contentExuserService.distributionBy(null,contentUnreviewedReq.getDyID(), (int)contentExusers.getTotalElements());
				} 
		        if(contentExusers.getNumberOfElements()>0){//
		        	redisTemplateUtil.set(RedisKey.last_red_key+contentUnreviewedReq.getDyID(),String.valueOf(contentExusers.getContent().get((contentExusers.getNumberOfElements())-1).getDyContextID()));
		        }else{
		        	redisTemplateUtil.del(RedisKey.last_red_key+contentUnreviewedReq.getDyID());
		        }
		        map.put("dyContexts",  contentExusers.getContent());
		    }else{
		    	map.put("dyContexts",new ArrayList<DyContexts>());
		    }
		}else{
			map.put("dyContexts",new ArrayList<DyContexts>());
		}
        map.put("respMessage", "OK");
        map.put("respCode", "0");
        map.put("isUsed", true);
		return map;
	}
	
	
	@PostMapping(value = { "/countent/examine" })
	public Object dyExamin(String dyId,String coutentId,String operator){
		ContentExuser contentExuser =new ContentExuser();
		contentExuser.setOperationCode(UUID.randomUUID().toString()); 
		contentExuser.setExResult(operator);
		contentExuser.setExTime(new Date());
		contentExuser.setExuserId(Long.valueOf("0"));
		contentExuser.setExdyId(dyId);
		contentExuser.setDyContentId(Long.valueOf(coutentId));
	    Map<String, Object> map=contentExuserService.updateExDyIdContent(contentExuser);
	     
		return map;
	} 
}
